<?php
namespace app\admin\controller;
use app\admin\model\Conf as ConfModel;
use app\admin\controller\Base;
class Conf extends Base
{
    public function lst(){
        if(request()->isPost()){
            $sorts = input('post.');
//            $conf = new ConfModel();
//            dump($sorts);die;
            foreach ($sorts as $k => $v) {
//                $conf->update(['id'=>$k,'sort'=>$v]);
//                ConfModel::where('enname',$k)->update(['value'=>$v]);
                ConfModel::where('id',$k)->update(['sort'=>$v]);
            }
            $this->success('更新排序成功！',url('lst'));
            return;
        }
        $confres = ConfModel::order('sort desc')->paginate(10);
        $this->assign('confres',$confres);
        return view();
    }

    public function add(){
        if(request()->isPost()){
            $data = input('post.');
            $validate = \think\Loader::validate('Conf');
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            if($data['values']){
                $data['values'] = str_replace('，', ',', $data['values']);
            }
            $conf = new ConfModel();
            if($conf->save($data)){
                $this->success('添加配置成功！',url('lst'));
            }else{
                $this->error('添加配置失败！');
            }
        }
        return view();
    }

    public function edit(){
        if(request()->isPost()){
            $data = input('post.');
            $validate = \think\Loader::validate('Conf');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            if($data['values']){
                $data['values'] = str_replace('，', ',', $data['values']);
            }
            $conf = new ConfModel();
            $save = $conf->save($data,['id'=>$data['id']]);
            if($save !== false){
                $this->success('修改配置成功！',url('lst'));
            }else{
                $this->error('修改配置失败！');
            }
        }
        $confs = ConfModel::find(input('id'));
        $this->assign('confs',$confs);
        return view();
    }

    public function del(){
        $del = ConfModel::destroy(input('id'));
        if($del){
           $this->success('删除配置项成功！',url('lst')); 
        }else{
            $this->error('删除配置项失败！');
        }
    }

    public function conf(){
        $conf = new ConfModel();
        if(request()->isPost()){
            //提交过来的数据 是一个二维数组
//            dump($_FILES);die;
            $data = input('post.');

//            dump($data);die;
            //转换成新的一维数组
            $formarr = array();
            foreach ($data as $k => $v) {
                $formarr[] = $k;
            }
            //表中的所有数据 是一个二维数组
            $_confarr = db('conf')->field('enname')->select();
            //转换成新的一维数组
            $confarr = array();
            foreach ($_confarr as $k => $v) {
                $confarr[] = $v['enname'];
            }
            //把存在的数据放置到新的数组里
            $checkboxarr = array();
            foreach ($confarr as $k => $v) {
                //判断原来的数据里是否存在提交过来的数据
                if(!in_array($v, $formarr)){
                    $checkboxarr[] = $v;
                }
            }
            //取消勾选之类的操作，就把该项的value清空
            if($checkboxarr){
                foreach ($checkboxarr as $ke => $v) {
//                    $conf->where('enname',$v)->update(['value'=>'']);
//                    ConfModel::where('enname',$v)->update(['value'=>'']);
                }
            }
            //正常途径有值就直接修改
            if($data){
//                dump($data);die;
                foreach ($data as $k=>$v) {
//                    dump($k);die;
                    ConfModel::where('enname',$k)->update(['value'=>$v]);
//                    $conf->updateAt(['value'=>$v],['enname'=>$k]);
//                    if($k == 'app' || $k == 'gzh'){
//
//                    }else{
//
//                    }


                }
                if($_FILES){
                    $conf->update(['enname'=>'logo'],['id'=>19]);
                    $conf->update(['enname'=>'app'],['id'=>10]);
                    $conf->update(['enname'=>'gzh'],['id'=>11]);
                }
//                die;
                $this->success('修改配置成功！');
            }
            return;
        }
        $confres = ConfModel::order('sort desc')->select();
        $this->assign('confres',$confres);
        return view();
    }





    

    




   

	












}
